<template>
  <view class="coupon_wrapper">
    <u-navbar :is-back="false" title="优惠券" :is-fixed="true" title-color="#000" :border-bottom="false">
      <view class="slot-wrap">
        <view class="navbar-right">
          <view class="message-box right-item" @click="back" style="margin-top: 6upx">
            <u-icon name="arrow-left" size="32"></u-icon>
          </view>
          <view class="line"></view>
          <navigator url="/pages/user/excharge_coupon" class="right-item rigth-txt">兑换优惠券</navigator>
        </view>
      </view>
    </u-navbar>
    <view>
      <u-tabs-swiper ref="uTabs" :list="list" :current="current" :bar-width="120" active-color="#2A6AAA" @change="tabsChange" :is-scroll="false"
                     swiperWidth="750">
      </u-tabs-swiper>
    </view>
    <swiper :current="swiperCurrent" @transition="transition"
            @animationfinish="animationfinish" class="swipper-wrp">
      <swiper-item class="swiper-item" v-for="(items, index) in tabs" :key="index">
        <scroll-view scroll-y style="height: 100%;width: 100%;padding-bottom: 20upx;background: #F7F7F7" @scrolltolower="onreachBottom">
          <div class="coupon_list_warpper">
            <div class="item_coupon" :class="{'inActive': index != 0}" v-for="item in items">
              <div class="coupon_top">
                <div class="left" v-if="item && item.compassCoupon && item.compassCoupon.couponType == 1">
                  <div class="number"><span>{{item.compassCoupon && item.compassCoupon.discount_percent}}</span>折</div>
                  <div class="desc">无金额门槛</div>
                </div>
                <div class="left" v-if="item && item.compassCoupon && item.compassCoupon.couponType == 2">
                  <div class="number">￥<span>{{item.compassCoupon && item.compassCoupon.discount}}</span></div>
                  <div class="desc">满￥{{item && item.compassCoupon && item.compassCoupon.couponCondition}}使用</div>
                </div>
                <div class="right">
                  <div class="right_top">
                    <div class="left_tag">{{item && item.compassCoupon && item.compassCoupon.couponType == 1?'折扣券':'满减券'}}</div>
                    <div>新高考选科测评服务{{item && item.compassCoupon && item.compassCoupon.couponType == 1?'折扣券':'满减券'}}</div>
                  </div>
                  <div class="right_bottom" v-if="index == 0">
                    <div  class="date">{{(item && item.couponEndTime)}}到期</div>
                    <div class="use_btn" @click="toGoods()">立即使用</div>
                  </div>
                </div>
              </div>
              <div class="bottom">
                使用条件：{{item && item.compassCoupon && item.compassCoupon.conditionDesc}}
              </div>
            </div>
            <div class="no-data-img" style="padding-top: 100upx" v-if="items.length < 1">
              <u-empty text="暂无此类优惠券" mode="coupon"></u-empty>
            </div>
          </div>
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
</template>

<script>
	import {request} from "../../util/api";

  export default {
		data() {
			return {
        list: [
          {
            name: '未使用'
          }, {
            name: '已使用'
          }, {
            name: '已过期'
          }
        ],
        tabs: [],
				current: 0,
				swiperCurrent: 0,
        couponList: [],
        status: 0
			};
		},

    onShow() {
      this.getOrders()
    },

		methods: {
      back() {
        uni.navigateBack()
      },
      getOrders() {
        request({
          url: '/app/coupon/get/user/couponList',
          method: 'post'
        }).then(ret => {
          if (ret.data) {
            this.couponList = ret.data;
            this.filterdata()
          }
        })
      },

      filterdata() {
        var tabs = []
        for (var i = 0; i < 3; i++ ) {
          let arr = []
          arr = this.couponList.filter(item=> {
            if (item.compassCoupon.couponType == 1) {
              item.compassCoupon.discount_percent = (item.compassCoupon.discount/10).toFixed(1)

            }
            return item.status == i
          })
          tabs.push(arr)
        }
        this.tabs = tabs
      },

			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			// swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			// scroll-view到底部加载更多
			onreachBottom() {

			},

      toGoods() {
        this.$u.route({
          url: '/pages/goods/index'
        })
      }
		}
	}
</script>

<style lang="scss" scoped>
  .coupon_wrapper {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--window-top));
    width: 100%;
    .swipper-wrp {
      flex: 1;
      box-sizing: border-box;
    }
    .coupon_list_warpper {
      width: 94%;
      margin: 0 auto;
      padding-top: 10upx;
    }
    .item_coupon {
      border-radius: 15upx;
      border: 3upx solid #2A6AAA;
      margin: 20upx 0;
      font-size: 26upx;
      background: rgb(156,210,244);
    }
    .coupon_top {
      display: flex;
    }
    .left {
      width: 250upx;
      text-align: center;
      border-right: 3upx dashed #2A6AAA;
    }
    .number span {
      font-size: 100upx;
      font-weight: bold;
    }
    .desc {
      margin-bottom: 20upx;
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-left: 20upx;
    }
    .right_top, .right_bottom {
      display: flex;
      margin: 10upx 0;
    }
    .right_bottom {
      justify-content: space-between
    }
    .date {
      font-size: 20upx;
      display: flex;
      align-items: center;
    }
    .left_tag {
      background: #2A6AAA;
      color: #fff;
      padding: 0 5upx;
      margin-right: 8upx;
      font-size: 20upx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 5upx;
    }
    .bottom {
      line-height: 80upx;
      text-indent: 40upx;
      border-top: 3upx dashed #2A6AAA;
    }
    .use_btn {
      background: #1F476D;
      color: #fff;
      padding: 10upx 20upx;
      font-size: 25upx;
      border-radius: 40upx;
      vertical-align: middle;
    }
    .inActive {
      background: rgb(242,242,242);
      border-color: rgb(219,219,219)
    }
    .inActive .left_tag{
      background: rgb(151,151,151)
    }
    .inActive .bottom, .inActive .left {
      border-color: rgb(219,219,219)
    }
  }
  .navbar-right {
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: 10px;
    border: 1px solid rgba(10,10,10,.1);
    background: rgba(255,255,255, .6);
    border-radius: 18px;
    padding-left: 5px;
    padding-right: 5px;
    margin-top: -3px;
    line-height: 32px;
  }
  .line {
    width: 1px;
    background: rgba(252, 252, 252, 0.3);
    background: rgba(0, 0, 0, 0.2);
    height: 18px;
  }
  .rigth-txt {
    font-size: 14px;
    padding: 0;
    line-height: 32px;
    padding-right: 5px;
    padding-right: 20upx;
    padding-left: 20upx;
  }
  .message-box {
    padding-left: 15upx;
    padding-right: 20upx;
  }
</style>
